import type { FormSchemaGetter } from '#/adapter/form';
import type { VxeGridProps } from '#/adapter/vxe-table';

import { DictEnum } from '@vben/constants';

import { Checkbox, InputNumber, RadioGroup, Tag } from 'ant-design-vue';

import { getDictOptions } from '#/utils/dict';
import { renderDict } from '#/utils/render';

export const querySchema: FormSchemaGetter = () => [
  {
    component: 'RangePicker',
    componentProps: {
      showTime: false,
      format: 'YYYY-MM-DD',
      valueFormat: 'YYYY-MM-DD',
    },
    fieldName: 'pickUpTime',
    label: '处理时间',
  },
];

export const columns: VxeGridProps['columns'] = [
  {
    title: '记录号',
    field: 'pickUpId',
    width: 180,
  },
  {
    title: '商品名称',
    field: 'saleProductName',
    width: 120,
  },
  {
    title: '值',
    field: 'pickUpNum',
    width: '100px',
  },
  {
    title: '处理时间',
    field: 'pickUpTime',
    width: 150,
  },
  {
    title: '证明材料',
    field: 'file',
    showOverflow: true,
    slots: { default: 'url' },
    width: 150,
  },
  {
    title: '退回值',
    field: 'returnNum',
    width: '120',
  },
  {
    title: '类型',
    field: 'pickUpType',
    slots: {
      default: ({ row }) => {
        if (row.pickUpType) {
          return renderDict(row.pickUpType, DictEnum.PICK_UP_TYPE);
        }
        return '-';
      },
    },
    width: 120,
  },
  {
    title: '状态',
    field: 'status',
    slots: {
      default: ({ row }) => {
        if (row.status) {
          return renderDict(row.status, DictEnum.PICK_UP_STATUS);
        }
        return '-';
      },
    },
    width: 120,
  },

  {
    title: '补充提货',
    field: 'isSupplementaryPickup',
    slots: {
      default: ({ row }) => {
        if (row.isSupplementaryPickup) {
          return row.isSupplementaryPickup ? (
            <Tag color="green">是</Tag>
          ) : (
            <Tag color="red">否</Tag>
          );
        }
        return '-';
      },
    },
    width: 120,
  },

  {
    field: 'action',
    fixed: 'right',
    slots: { default: 'action' },
    title: '操作',
    width: 180,
  },
];

export const modalSchema: FormSchemaGetter = () => [
  {
    label: '提货记录id',
    fieldName: 'pickUpId',
    component: 'Input',
    dependencies: {
      show: () => false,
      triggerFields: [''],
    },
  },
  {
    label: '赠品集合',
    fieldName: 'giftList',
    component: 'Input',
    dependencies: {
      show: () => false,
      triggerFields: [''],
    },
  },
  {
    label: '最大提货数量',
    fieldName: 'maxPickUpNum',
    component: 'InputNumber',
    componentProps: {
      precision: 2,
    },
    dependencies: {
      show: () => false,
      triggerFields: [''],
    },
  },
  {
    label: '最大退款金额',
    fieldName: 'maxRefundAmount',
    component: 'InputNumber',
    componentProps: {
      precision: 2,
    },
    dependencies: {
      show: () => false,
      triggerFields: [''],
    },
  },
  {
    label: '最大回款金额',
    fieldName: 'maxRefundSurplusNum',
    component: 'InputNumber',
    componentProps: {
      precision: 2,
    },
    dependencies: {
      show: () => false,
      triggerFields: [''],
    },
  },
  {
    label: '最大提货数量',
    fieldName: 'maxSurplusNum',
    component: 'InputNumber',
    componentProps: {
      precision: 2,
    },
    dependencies: {
      show: () => false,
      triggerFields: [''],
    },
  },
  {
    label: '是否评点',
    fieldName: 'isGift',
    component: 'Input',
    dependencies: {
      show: () => false,
      triggerFields: [''],
    },
  },
  {
    label: '销售订单',
    fieldName: 'saleOrderId',
    component: 'Select',
    componentProps: {},
    dependencies: {
      show: () => false,
      triggerFields: [''],
    },
    rules: 'selectRequired',
  },
  {
    label: '业务类型',
    fieldName: 'pickUpType',
    component: 'RadioGroup',
    componentProps: {
      options: getDictOptions(DictEnum.PICK_UP_TYPE),
      buttonStyle: 'solid',
      optionType: 'button',
    },
    dependencies: {
      async componentProps(values) {
        return {
          async onChange() {
            // 重置其余选项的值
            values.pickUpNum = undefined;
            values.productType = undefined;
            values.giftProductId = undefined;
            values.returnType = undefined;
            values.isGiftReturn = undefined;
            values.giftReturnType = undefined;
          },
        };
      },
      triggerFields: [''],
    },
    rules: 'selectRequired',
  },
  // 新增退货类型选择
  {
    label: '退货类型',
    fieldName: 'returnType',
    component: 'RadioGroup',
    componentProps: {
      options: [
        { label: '部分退', value: '1' },
        { label: '全部退', value: '2' },
        { label: '仅退款', value: '3' },
        { label: '仅退货', value: '4' },
      ],
      buttonStyle: 'solid',
      optionType: 'button',
    },
    dependencies: {
      show: (value) => value.pickUpType === '3',
      triggerFields: ['pickUpType'],
    },
    rules: 'selectRequired',
  },
  // 修改原有的数量/金额字段
  {
    label: '退货数量',
    fieldName: 'pickUpNum',
    component: 'InputNumber',
    dependencies: {
      show: (value) =>
        value.pickUpType == '3' &&
        (value.returnType == '1' || value.returnType == '4'),
      componentProps(value) {
        return {
          min: 1,
          max: value.maxPickUpNum,
          precision: 2,
        };
      },
      triggerFields: ['pickUpType', 'returnType'],
    },
    rules: 'required',
  },
  {
    label: '退款金额',
    fieldName: 'refundAmount',
    component: 'InputNumber',
    dependencies: {
      show: (value) =>
        value.pickUpType == '3' &&
        (value.returnType == '1' || value.returnType == '3'),
      componentProps(value) {
        return {
          min: 0,
          max: value.maxRefundAmount,
          precision: 2,
        };
      },
      triggerFields: ['pickUpType', 'returnType'],
    },
    rules: 'required',
  },
  {
    label: '商品类型',
    fieldName: 'productType',
    component: 'RadioGroup',
    defaultValue: 'CP',
    componentProps: {
      options: getDictOptions(DictEnum.HM_PRODUCT_TYPE),
      buttonStyle: 'solid',
      optionType: 'button',
    },
    dependencies: {
      show: (value) => {
        return value.pickUpType === '1' && value.isGift === 'Y';
      },
      componentProps(value) {
        return {
          async onChange() {
            // 重置其余选项的值
            value.pickUpNum = undefined;
            value.giftProductName = undefined;
            value.giftProductId = undefined;
          },
        };
      },
      triggerFields: ['pickUpType', 'isGift'],
    },
    rules: 'selectRequired',
  },
  {
    label: '商品id',
    fieldName: 'saleProductId',
    component: 'Input',
    dependencies: {
      show: () => false,
      disabled: () => true,
      triggerFields: ['productType'],
    },
    rules: 'selectRequired',
  },
  {
    label: '商品名称',
    fieldName: 'saleProductName',
    component: 'Input',
    dependencies: {
      disabled: () => true,
      show: (value) => value.productType === 'CP' && value.pickUpType === '1',
      triggerFields: ['productType', 'pickUpType'],
    },
  },
  {
    label: '赠品',
    fieldName: 'giftProductId',
    component: 'RadioGroup',
    rules: 'selectRequired',
    componentProps: {
      buttonStyle: 'solid',
      optionType: 'button',
    },
    dependencies: {
      show: (value) => value.productType === 'ZP' && value.pickUpType === '1',
      triggerFields: ['productType', 'pickUpType', 'isGiftReturn'],
    },
  },
  // 修改原有的数量/金额字段
  {
    label: '提货数量',
    fieldName: 'pickUpNum',
    component: 'InputNumber',
    dependencies: {
      show: (value) => value.pickUpType == '1',
      componentProps(value) {
        if (value.giftProductId) {
          // 赠品提货,提货数量的最大值为赠品的剩余提货数量
          const gift = value.giftList.find(
            (item: any) => item.value === value.giftProductId,
          );
          return {
            min: 1,
            max: gift.maxExtractQty,
            precision: 2,
          };
        }
        return {
          min: 1,
          max: value.maxSurplusNum,
          precision: 2,
        };
      },
      triggerFields: [
        'pickUpType',
        'productType',
        'returnType',
        'giftProductId',
      ],
    },
    rules: 'required',
  },
  {
    label: '回款金额',
    fieldName: 'pickUpNum',
    component: 'InputNumber',
    dependencies: {
      show: (value) => value.pickUpType == '0',
      componentProps(value) {
        return {
          min: 1,
          max: value.maxRefundSurplusNum,
        };
      },
      triggerFields: ['pickUpType', 'returnType'],
    },
    rules: 'required',
  },
  // 新增赠品退货选项
  {
    label: '赠品退货',
    fieldName: 'isGiftReturn',
    component: 'RadioGroup',
    componentProps: {
      options: getDictOptions(DictEnum.SYS_YES_NO),
      buttonStyle: 'solid',
      optionType: 'button',
    },
    dependencies: {
      show: (value) => value.pickUpType === '3' && value.giftList.length > 0,
      triggerFields: ['pickUpType'],
    },
    rules: 'selectRequired',
  },
  {
    label: '退货赠品',
    fieldName: 'giftReturns',
    component: 'Space',
    renderComponentContent: (model) => {
      // 实际应从订单数据获取赠品列表，这里用模拟数据
      const gifts = model.giftList;
      if (gifts === undefined) {
        return {
          default: () => <div>无赠品</div>,
        };
      }
      return {
        // 使用默认插槽渲染复杂内容
        default: () => (
          <div class="space-y-4">
            {gifts.map((gift, index) => (
              <div class="flex items-center gap-4">
                <Checkbox
                  onChange={(e) => {
                    model.giftReturns = model.giftReturns || [];
                    model.giftReturns[index] = {
                      ...model.giftReturns[index],
                      selected: e.target.checked,
                      saleProductId: gift.value,
                    };
                  }}
                  value={model.giftReturns?.[index]?.selected}
                >
                  {gift.label}
                </Checkbox>

                {model.giftReturns?.[index]?.selected && (
                  <div class="ml-6 flex gap-2">
                    <RadioGroup
                      onChange={(e) => {
                        model.giftReturns[index].refundType = e.target.value;
                        if (e.target.value === '2') {
                          model.giftReturns[index].returnNum = gift.maxQty;
                        }
                      }}
                      options={[
                        { label: '部分退货', value: '1' },
                        { label: '全部退货', value: '2' },
                      ]}
                      value={model.giftReturns[index]?.refundType}
                    />

                    {model.giftReturns[index]?.refundType === '1' && (
                      <InputNumber
                        max={gift.maxQty}
                        min={1}
                        onChange={(value) => {
                          model.giftReturns[index].returnNum = value;
                        }}
                        placeholder="数量"
                        value={model.giftReturns[index]?.returnNum}
                      />
                    )}
                  </div>
                )}
              </div>
            ))}
          </div>
        ),
      };
    },
    dependencies: {
      show: (value) => value.isGiftReturn === 'Y' && value.pickUpType === '3',
      triggerFields: ['isGiftReturn', 'pickUpType'],
    },
  },

  {
    label: '处理时间',
    fieldName: 'pickUpTime',
    component: 'DatePicker',
    componentProps: {
      showTime: true,
      format: 'YYYY-MM-DD HH:mm:ss',
      valueFormat: 'YYYY-MM-DD HH:mm:ss',
    },
    rules: 'required',
  },
  {
    label: '证明材料',
    fieldName: 'file',
    component: 'ImageUpload',
    componentProps: {
      // accept: ['jpg'], // 不支持type/*的写法 支持拓展名(不带.) 文件头(image/png这种)
      // maxNumber: 2, // 最大上传文件数 默认为1 为1会绑定为string而非string[]类型
      resultField: 'url', // 上传成功后返回的字段名 默认url 可选['ossId', 'url', 'fileName']
    },
    rules: 'required',
  },
  {
    label: '状态',
    fieldName: 'status',
    component: 'RadioGroup',
    componentProps: {
      buttonStyle: 'solid',
      optionType: 'button',
    },
    defaultValue: '0', // 未确认TODO 此处是否需要审核
    dependencies: {
      show: () => false,
      triggerFields: [''],
    },
  },
  {
    label: '生成待办',
    fieldName: 'isCreateTodo',
    component: 'RadioGroup',
    defaultValue: 'Y',
    componentProps: {
      options: getDictOptions(DictEnum.SYS_YES_NO),
      buttonStyle: 'solid',
      optionType: 'button',
    },
    rules: 'selectRequired',
    dependencies: {
      show: () => false,
      triggerFields: ['pickUpType'],
    },
  },
  {
    label: '备注',
    fieldName: 'remark',
    component: 'Input',
    // rules: 'required',
  },
];
